<template>
	<view class="container">
		<view class="ziliao">
			<view class="item" style="border:1rpx solid #f1f1f1">
				<view class="left">
					<text style="color: red;">*</text>
					<text style="font-size: 32rpx;">昵称</text>
				</view>
				<view class="right">
					<input type="nickname" v-model="nickname" placeholder="请输入昵称"
						@change="nicknameChange" style="font-size:32rpx;width: 260rpx;text-align: right;" />
				</view>
			</view>
			<view class="item" style="border:1rpx solid #f1f1f1">
				<view class="left">
					<text style="color: red;">*</text>
					<text style="font-size: 32rpx;">头像</text>
				</view>
				<view style="display: flex;align-items: center;" >
					<button open-type="chooseAvatar" plain="true" class="custom-button" @chooseavatar="chooseAvatar">
						<image :src="baseHttp+avatar" style="height: 100rpx;width: 100rpx;"></image>
					</button>
					<view style="font-size: 50rpx;margin-left: 10rpx;color: #999;">></view>
				</view>
			</view>
		</view>
	<view class="submit">
		<view class="btn" @click="submit">立即保存</view>
	</view>
	</view>
	
	
	
</template>

<script>
	const app = getApp()
	import Http from "@/utils/http"
	const http = new Http()
	export default {
		data() {
			return {
               nickname:"",
			   avatar:"",
			   baseHttp:""
			}
		},
	  async onLoad() {
			this.baseHttp = app.globalData.baseHttp
			let res = await http.get("v1/user/getUserInfo")
			this.avatar = res.data.data.avatar
			this.nickname = res.data.data.nickname
			
		},
		methods: {
			async chooseAvatar(e) {
                let res = await http.upload(e.detail.avatarUrl)
				console.log("====",res.data);
				let result = JSON.parse(res.data)
				this.avatar = result.result.name
			},
			async submit(){
				console.log("nickname",this.nickname);
				console.log("avatar",this.avatar);
				if(!this.nickname){
					return uni.showToast({
						title:"昵称不能为空",
						icon:"none"
					})
				}
				if(!this.avatar){
					return uni.showToast({
						title:"头像不能为空",
						icon:"none"
					})
				}
				let res = await http.post("v1/user/updateAvatarNickname",{
					avatar:this.avatar,
					nickname:this.nickname,
				})
				console.log("保存",res);
				uni.showToast({title:"保存成功"})
				uni.switchTab({
					url:'/pages/Person/Person'
				})
				
				
			},
			nicknameChange(e){
				console.log("昵称",e);
				this.nickname = e.detail.value
			}
		}
	}
</script>

<style scoped lang="scss">
	page {
		background: #f1f1f1;
	}

	.ziliao {
		margin: 50rpx 30rpx;
		background: #fff;
		border-radius: 12rpx;

		.item {
			display: flex;
			justify-content: space-between;
			padding: 20rpx;

			.left {
				display: flex;
				align-items: center;

			}

			.right {
				width: 260rpx;
			}
		}
	}

	button::after {
		border: none;
	}

	button {
		background-color: rgb(247, 247, 247);
		//可根据自己的背景颜色自行设置
		border-radius: 0rpx;
		padding: 0rpx;
		margin: 0rpx;
		display: inline-block;
		line-height: 1;
		border: 1rpx solid #fff;
	}

	button::after {
		/*button的边框样式是通过::after方式实现的*/
		border: none;
	}

	.button-hover {
		/*点击后样式*/
		color: #000;
		background-color: rgb(247, 247, 247);
	}
	.submit{
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background: #fff;
		height: 120rpx;
		.btn{
			margin: 20rpx 30rpx;
			background: #00008B;
			height: 80rpx;
			line-height: 80rpx;
			border-radius: 40rpx;
			color: #fff;
          text-align: center;			
		}
	}
</style>